<template>
	<el-card class="layout-query">
		<el-form ref="queryRef" :inline="true" :model="state.queryForm" @keyup.enter="getDataList()">
			<el-form-item>
				<el-input v-model="state.queryForm.search" placeholder="请输入景区名称" clearable></el-input>
			</el-form-item>
			<el-form-item>
				<el-button icon="Search" type="primary" @click="getDataList()">查询</el-button>
			</el-form-item>
			<el-form-item>
				<el-button icon="RefreshRight" @click="reset(queryRef)">重置</el-button>
			</el-form-item>
		</el-form>
	</el-card>

	<el-card>
		<el-space>
			<el-space>
				<el-button icon="Plus" type="primary" @click="addOrUpdateHandle()">新增</el-button>
			</el-space>
			<el-space>
				<el-button plain icon="Download" @click="downloadHandle('/ss/scenic/export')">导出</el-button>
			</el-space>
			<el-space>
				<el-button icon="Delete" plain type="danger" @click="deleteBatchHandle()">批量删除</el-button>
			</el-space>
		</el-space>
		<el-table v-loading="state.dataListLoading" :data="state.dataList" border class="layout-table"
			show-overflow-tooltip @selection-change="selectionChangeHandle">
			<el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
			<el-table-column prop="name" label="景区名称" header-align="center" align="center"></el-table-column>
			<el-table-column prop="phone" label="手机号" header-align="center" align="center"></el-table-column>
			<el-table-column prop="principal" label="负责人" header-align="center" align="center"></el-table-column>
			<el-table-column prop="rule" label="收费规则" header-align="center" align="center">
				<template #default="scope">
					<el-button type="primary" link @click="handleRuleClick(scope.row)">
						{{ scope.row.rule === 1 ? '查看' : '去设置' }}
					</el-button>
				</template>
			</el-table-column>
			<el-table-column prop="num" label="所属车辆" header-align="center" align="center">
				<template #default="scope">
					{{ scope.row.num }}
				</template>
			</el-table-column>
			<el-table-column prop="address" label="地址" header-align="center" align="center">
				<template #default="scope">
					{{ scope.row.province + scope.row.city + scope.row.area + scope.row.address }}
				</template>
			</el-table-column>
			<el-table-column prop="isRun" label="是否运营" header-align="center" align="center">
				<template #default="scope">
					{{ scope.row.isRun === 1 ? '运行中' : '暂停' }}
				</template>
			</el-table-column>
			<el-table-column label="操作" fixed="right" header-align="center" align="center" width="150">
				<template #default="scope">
					<el-dropdown>
						<el-button type="primary" link>
							操作<el-icon class="el-icon--right"><arrow-down /></el-icon>
						</el-button>
						<template #dropdown>
							<el-dropdown-menu>
								<el-dropdown-item @click="handleOverrunSetting(scope.row)">超区设置</el-dropdown-item>
								<el-dropdown-item @click="">规则设置</el-dropdown-item>
								<!-- <el-dropdown-item @click="handleRuleSetting(scope.row)">规则设置</el-dropdown-item> -->
								<el-dropdown-item @click="">区域设置</el-dropdown-item>
								<!-- <el-dropdown-item @click="addOrUpdateHandle(scope.row.id)">区域设置</el-dropdown-item> -->
								<el-dropdown-item @click="handlePause(scope.row)">{{
									scope.row.isRun == 1 ? "暂停" : "开始"
								}}运营</el-dropdown-item>
								<el-dropdown-item @click="addOrUpdateHandle(scope.row.id)">修改设置</el-dropdown-item>
								<el-dropdown-item @click="deleteBatchHandle(scope.row.id)"
									style="color: var(--el-color-danger)">进行删除</el-dropdown-item>
							</el-dropdown-menu>
						</template>
					</el-dropdown>
				</template>
			</el-table-column>
		</el-table>
		<el-dialog 
  v-model="overrunSettingVisible" 
  title="超区设置" 
  width="500px"
>
  <el-form label-width="150px">
    <el-form-item label="单笔订单通电次数">
      <el-input-number 
        v-model="overrunSettingForm.singleOrderOverrunCount" 
        :min="0" 
        controls-position="right"
      />
    </el-form-item>
    <el-form-item label="单次上电通电分钟">
      <el-input-number 
        v-model="overrunSettingForm.singleChargeOverrunDuration" 
        :min="0" 
        controls-position="right"
      />
    </el-form-item>
  </el-form>
  <template #footer>
    <el-button @click="overrunSettingVisible = false">取 消</el-button>
    <el-button type="primary" @click="confirmOverrunSetting">确 定</el-button>
  </template>
</el-dialog>
		<el-pagination :current-page="state.page" :page-sizes="state.pageSizes" :page-size="state.limit"
			:total="state.total" layout="total, sizes, prev, pager, next, jumper" @size-change="sizeChangeHandle"
			@current-change="currentChangeHandle">
		</el-pagination>
		<!-- 弹窗, 新增 / 修改 -->
		<add-or-update v-if="addOrUpdateVisible" ref="addOrUpdateRef" v-model:visible="addOrUpdateVisible"
			@refreshDataList="getDataList"></add-or-update>
	</el-card>
</template>

<script setup lang="ts" name="OrderScenicIndex">
import { useCrud } from '@/hooks'
import { reactive, nextTick, ref } from 'vue'
import { IHooksOptions } from '@/hooks/interface'
import { useRouter } from 'vue-router'
import AddOrUpdate from './add-or-update.vue'
import { ElMessage, ElMessageBox, SCOPE } from 'element-plus'
import { useOrderScenicApi, useOrderScenicSubmitApi } from '@/api/order/scenic'
const router = useRouter()

const state: IHooksOptions = reactive({
	dataListUrl: '/ss/scenic/page',
	deleteUrl: '/ss/scenic',
	queryForm: {

	}
})

const overrunSettingVisible = ref(false)
const overrunSettingForm = reactive({
  singleOrderOverrunCount: 0,  // 单笔订单超区通电次数
  singleChargeOverrunDuration: 0  // 单次上电通电时长
})
const handleOverrunSetting = (row: any) => {
  // 初始化表单数据
  overrunSettingForm.singleOrderOverrunCount = row.singleOrderOverrunCount || 0
  overrunSettingForm.singleChargeOverrunDuration = row.singleChargeOverrunDuration || 0
  
  overrunSettingVisible.value = true
}
const confirmOverrunSetting = () => {
  // 校验数据
  if (overrunSettingForm.singleOrderOverrunCount < 0 || overrunSettingForm.singleChargeOverrunDuration < 0) {
    ElMessage.error('设置值不能为负数')
    return
  }
  // 调用接口保存设置
  useOrderScenicSubmitApi({
    id: currentRow.value.id,
    ...overrunSettingForm
  }).then(() => {
    ElMessage.success('超区设置保存成功')
    overrunSettingVisible.value = false
    getDataList() // 刷新列表
  }).catch(error => {
    ElMessage.error('保存失败：' + error.message)
  })
}
const currentRow = ref<any>(null)

const queryRef = ref()
const addOrUpdateVisible = ref(false)
const addOrUpdateRef = ref()
const addOrUpdateHandle = (id?: number) => {
	addOrUpdateVisible.value = true
	nextTick(() => addOrUpdateRef.value.init(id))
}

// 收费规则点击处理方法
const handleRuleClick = (row: any) => {
  if (row.rule === 1) {
    // 查看逻辑，跳转到查看页面
    router.push({
      path: '/order/costRule/index',
      query: { scenicId: row.id }
    })
  } else {
    // 去设置逻辑，跳转到设置页面
    router.push({
      path: '/order/costRule/index',
      query: { scenicId: row.id }
    })
  }
}


// 暂停运营处理方法
const handlePause = async (row: any) => {
	await ElMessageBox.confirm(`确定要暂停【${row.name}】的运营吗?`, '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning'
	})
	row.isRun = row.isRun == 1 ? 0 : 1;
	useOrderScenicSubmitApi(row).then(() => {
		ElMessage.success({
			message: '操作成功',
			duration: 500,
			onClose: () => {
				getDataList();
			}
		})
	})
}

const { getDataList, selectionChangeHandle, sizeChangeHandle, currentChangeHandle, deleteBatchHandle, downloadHandle, reset } = useCrud(state)
</script>
